﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// qt headers
 #include <QApplication>
 #include <QStackedLayout>
 #include <QDesktopWidget>
 #include <QEvent>
 #include <QKeyEvent>
 #include <QPainter>
 #include <QIcon>
 #include <QPushButton>
 #include <QFrame>

// custom headers
#include "GcsMainWindow.h"
#include "TitleBar.h"
#include "ProjectPage.h"
#include "ProjectComponentEditingPage.h"
#include "IDetailedProjectComponent.h"
#include "TranslatorManager.h"
#include "GbmpNew.h"
#include "GncINetworkCheck.h"
#include "BnufBulletin.h"
#include "GmepRecordTimeUtils.h"
#include "GmepVersionUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs 
{
    MainWindow::MainWindow(QWidget * parent/* = nullptr*/) :
        QWidget(parent),
        m_pTitleBar(nullptr),
        m_pPagesStackedLayout(nullptr),
        m_pProjectPage(nullptr),
        m_bFirstShow(true),
        m_isColloaborationDocument(true)
    {
        Init();
    }

    MainWindow::~MainWindow()
    {
    }

    void MainWindow::SetTitleTip(QString tip)
    {
       if(m_pTitleBar != nullptr)
            m_pTitleBar->SetPinTip(tip);
    }

    void MainWindow::RefreshCurrentPage(bool isCollaborationDocument /*= true*/)
    {
        m_isColloaborationDocument  = isCollaborationDocument;
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr, L"m_pPagesStackedLayout为空", L"GMEP", L"2024-01-30");

        auto currentPageIndex = m_pPagesStackedLayout->currentIndex();
        switch (currentPageIndex)
        {
        case PROJECT_PAGE_INDEX:
        {
            auto projectPage = static_cast<ProjectPage*>(m_pPagesStackedLayout->widget(PROJECT_PAGE_INDEX));
            if (projectPage)
            {
                projectPage->Refresh(m_isColloaborationDocument);
            }

            break;
        }
        default:
            GotoProjectPage();
        }

    }


    bool MainWindow::eventFilter(QObject * obj, QEvent * event)
    {
        if (event)
        {
            switch (event->type()) {
            case QEvent::KeyPress: {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                if (keyEvent && keyEvent->key() == Qt::Key_Return)
                {

                }
                break;
            }
            default: {
                break;
            }
            }

            return QWidget::eventFilter(obj, event);
        }
        return false;
    }

    void MainWindow::paintEvent(QPaintEvent * event)
    {
        QWidget::paintEvent(event);
        if (event)
        {
            QPainter painter(this);

            // draw border
            painter.setPen(QPen(QColor(189, 199, 213, 255), 1.0));
            painter.drawLine(rect().bottomLeft(), rect().topLeft());
            painter.drawLine(rect().topLeft(), rect().topRight());
            painter.drawLine(rect().topRight(), rect().bottomRight());
            painter.drawLine(rect().bottomRight(), rect().bottomLeft());
        }
    }

    void MainWindow::showEvent(QShowEvent * event)
    {
        QWidget::showEvent(event);

        if (m_bFirstShow)
        {
            QWidget* pWidget = parentWidget();
            QRect screenRect;
            if (pWidget)
            {
                screenRect = pWidget->frameGeometry();
            }
            move((screenRect.width() - width()) / 2 + screenRect.x(), (screenRect.height() - height()) / 2 + screenRect.y());
            m_bFirstShow = false;
        }
    }

    void MainWindow::Init()
    {
        bool isPrivateEnv = gmep::GmepVersionUtils::IsPrivatizationEnvironment();

        // main window self
        setWindowModality(Qt::ApplicationModal);
        setObjectName("GcsMainWindow");

        setWindowFlags(Qt::FramelessWindowHint | windowFlags());
        //resize(1280, 720);
        setFixedWidth(1200);
        setFixedHeight(720);
        QPalette palette = (this->palette());
        palette.setColor(QPalette::Background, QColor(240, 240, 245, 255));
        setAutoFillBackground(true);
        setPalette(palette);

        // main layout
        QVBoxLayout *pMainLayout = NEW_AS_QT_CHILD(QVBoxLayout, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pMainLayout != nullptr, L"pMainLayout为空", L"GMEP", L"2024-01-30");
        setLayout(pMainLayout);
        pMainLayout->setContentsMargins(1, 1, 1, 1);
        pMainLayout->setSpacing(0);

        // title bar
        m_pTitleBar = NEW_AS_QT_CHILD(TitleBar,this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pTitleBar != nullptr, L"m_pTitleBar为空", L"GMEP", L"2024-01-30");
        pMainLayout->addWidget(m_pTitleBar);
        m_pTitleBar->setObjectName("GcsMainWindowTitleBar");
        installEventFilter(m_pTitleBar);
        setWindowTitle(QString::fromStdWString(GBMP_TR(L"项目构件管理")));
        setWindowIcon(QIcon(":/images/Gnmep.png"));
        pMainLayout->setAlignment(m_pTitleBar, Qt::AlignTop);
        palette.setColor(QPalette::Background, QColor(176, 178, 185));
        m_pTitleBar->setAutoFillBackground(true);
        m_pTitleBar->setPalette(palette);
        m_pTitleBar->SetMinBoxVisible(false);
        connect(m_pTitleBar, &TitleBar::moveRequested, this, &MainWindow::onMoveRequested);


        // centralWidget
        auto pCentralWidget = NEW_AS_QT_CHILD(QWidget, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCentralWidget != nullptr, L"pCentralWidget为空", L"GMEP", L"2024-01-30");
        pMainLayout->addWidget(pCentralWidget);

        // central stacked layout
        m_pCentralStackLayout = NEW_AS_QT(QStackedLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCentralStackLayout != nullptr, L"m_pCentralStackLayout为空", L"GMEP", L"2024-01-30");
        pCentralWidget->setLayout(m_pCentralStackLayout);
        m_pCentralStackLayout->setContentsMargins(0, 0, 0, 0);
        m_pCentralStackLayout->setSpacing(0);
        m_pCentralStackLayout->setStackingMode(QStackedLayout::StackAll);

        // pages widget
        auto pPagesWidget = NEW_AS_QT_CHILD(QWidget,pCentralWidget);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pPagesWidget != nullptr, L"pPagesWidget为空", L"GMEP", L"2024-01-30");
        m_pCentralStackLayout->addWidget(pPagesWidget);
        m_pCentralStackLayout->setCurrentIndex(PAGES_INDEX);

        // pages stacked layout
        m_pPagesStackedLayout = NEW_AS_QT(QStackedLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr, L"m_pPagesStackedLayout为空", L"GMEP", L"2024-01-30");
        pPagesWidget->setLayout(m_pPagesStackedLayout);
        m_pPagesStackedLayout->setContentsMargins(0, 0, 0, 0);
        m_pPagesStackedLayout->setSpacing(0);
        m_pPagesStackedLayout->setStackingMode(QStackedLayout::StackOne);
                
        // project page
        m_pProjectPage = NEW_AS_QT(ProjectPage,pPagesWidget, isPrivateEnv);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pProjectPage != nullptr, L"m_pProjectPage为空", L"GMEP", L"2024-01-30");
        m_pPagesStackedLayout->addWidget(m_pProjectPage);
        m_pProjectPage->setObjectName("GcsProjectPage");
        m_pPagesStackedLayout->setCurrentIndex(PROJECT_PAGE_INDEX);
        connect(m_pProjectPage, &ProjectPage::OnProjectComponentEditingRequired, this, &MainWindow::LaunchEditingProjectComponent);

        // project component editing page
        ProjectComponentEditingPage *pProjectComponentEditingPage = NEW_AS_QT_CHILD(ProjectComponentEditingPage,pPagesWidget, isPrivateEnv);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pProjectComponentEditingPage != nullptr, L"pProjectComponentEditingPage为空", L"GMEP", L"2024-01-30");
        m_pPagesStackedLayout->addWidget(pProjectComponentEditingPage);
        pProjectComponentEditingPage->setObjectName("GcsProjectComponentEditingPage");
        connect(pProjectComponentEditingPage, &ProjectComponentEditingPage::EditingFinished, this, &MainWindow::HandleProjectComponentEditingFinished);
        connect(pProjectComponentEditingPage, &ProjectComponentEditingPage::OnProjectComponentEditingPageRequired, this, &MainWindow::GotoProjectComponentEditingPage);
        
        // mask
        auto pMask = NEW_AS_QT_CHILD(QWidget,pCentralWidget);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pMask != nullptr, L"pMask为空", L"GMEP", L"2024-01-30");

        m_pCentralStackLayout->addWidget(pMask);
        pMask->setObjectName("MainWindowMask");
        pMask->setStyleSheet(
            "QWidget#MainWindowMask{"
            "background:transparent;"
            "}"
        );
        pMask->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    }
    
    void MainWindow::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
    {
        Q_UNUSED(cursorPos);
        move(pos() + offset);
    }


    void MainWindow::GotoProjectPage()
    {
        HideMask();
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr, L"m_pPagesStackedLayout为空", L"GMEP", L"2024-01-30");
        m_pPagesStackedLayout->setCurrentIndex(PROJECT_PAGE_INDEX);
        //m_pTitleBar->SetPinTip(QString());

        ProjectPage *projectPage = static_cast<ProjectPage*>(m_pPagesStackedLayout->widget(PROJECT_PAGE_INDEX));
        if (projectPage)
        {
            projectPage->Refresh(m_isColloaborationDocument);
        }
    }

    void MainWindow::ShowProjectComponentEditingPage(const std::wstring &projectId, const std::wstring &enterpriseId, const std::wstring &componentId, const int32_t &componentVersion, const std::wstring &componentName)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr && m_pTitleBar != nullptr, L"m_pPagesStackedLayout或m_pTitleBar 为空", L"GMEP", L"2024-01-30");

        HideMask();
        m_pPagesStackedLayout->setCurrentIndex(PROJECT_COMPONENT_EDITING_PAGE_INDEX);
        m_pTitleBar->SetPinTip(QString::fromStdWString(GBMP_TR(L"正在编辑项目构件")));

        ProjectComponentEditingPage *projectComponentEditingPage = static_cast<ProjectComponentEditingPage*>(m_pPagesStackedLayout->widget(PROJECT_COMPONENT_EDITING_PAGE_INDEX));
        if (projectComponentEditingPage)
        {
            projectComponentEditingPage->CheckIn(projectId, enterpriseId, componentId, componentVersion, componentName);
        }
    }


    void MainWindow::SetIsByEditComponentShow(const bool& isByEditCommonent)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout, L"m_pPagesStackedLayout为空", L"GMEP", L"2024-01-30");
        ProjectComponentEditingPage *projectComponentEditingPage = static_cast<ProjectComponentEditingPage*>(m_pPagesStackedLayout->widget(PROJECT_COMPONENT_EDITING_PAGE_INDEX));
        if (projectComponentEditingPage)
        {
            projectComponentEditingPage->SetIsByCommandShow(isByEditCommonent);
        }
    }

    void MainWindow::SetProjectComponentEditingPageCurrentFamilyTypeName(const std::wstring &familyTypeName)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout, L"m_pPagesStackedLayout为空", L"GMEP", L"2024-01-30");
        ProjectComponentEditingPage *projectComponentEditingPage = static_cast<ProjectComponentEditingPage*>(m_pPagesStackedLayout->widget(PROJECT_COMPONENT_EDITING_PAGE_INDEX));
        if (projectComponentEditingPage)
        {
            projectComponentEditingPage->SetComponentCurrentFamilyTypeName(familyTypeName);
        }
    }

    void MainWindow::ShowMask()
    {
        if (m_pCentralStackLayout != nullptr)
            m_pCentralStackLayout->setCurrentIndex(MAINWINDOW_MASK_INDEX);
    }

    void MainWindow::HideMask()
    {
        if (m_pCentralStackLayout != nullptr)
            m_pCentralStackLayout->setCurrentIndex(PAGES_INDEX);
    }

    void MainWindow::RefreshProjectPage()
    {
        if(m_pProjectPage != nullptr)
            m_pProjectPage->Refresh();
    }

    void MainWindow::GotoProjectComponentEditingPage()
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr && m_pTitleBar != nullptr, L"m_pPagesStackedLayout或m_pTitleBar 为空", L"GMEP", L"2024-01-30");

        HideMask();
        m_pPagesStackedLayout->setCurrentIndex(PROJECT_COMPONENT_EDITING_PAGE_INDEX);
        m_pTitleBar->SetPinTip(QString::fromStdWString(GBMP_TR(L"正在编辑项目构件")));

        ProjectComponentEditingPage *projectComponentEditingPage = static_cast<ProjectComponentEditingPage*>(m_pPagesStackedLayout->widget(PROJECT_COMPONENT_EDITING_PAGE_INDEX));
        if (projectComponentEditingPage)
        {            
        }
    }

    void MainWindow::HandleProjectComponentEditingFinished(const std::wstring & projectId, const std::wstring &componentId, const int32_t &componentVersion, const bool &isCommandShow)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr && m_pTitleBar != nullptr, L"m_pPagesStackedLayout或m_pTitleBar 为空", L"GMEP", L"2024-01-30");

        ProjectPage *projectPage = static_cast<ProjectPage*>(m_pPagesStackedLayout->widget(PROJECT_PAGE_INDEX));
        if (projectPage)
        {
            projectPage->RefreshComponent(projectId, componentId, componentVersion);
        }

        if (isCommandShow)
        {
            close();
            return;
        }

        HideMask();
        m_pPagesStackedLayout->setCurrentIndex(PROJECT_PAGE_INDEX);
        m_pTitleBar->SetPinTip(QString());
        
    }

    void MainWindow::LaunchEditingProjectComponent(const std::wstring & projectId, const std::wstring &enterpriseId, const std::wstring & componentId, const int32_t & componentVersion, const std::wstring &componentName)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pPagesStackedLayout != nullptr, L"m_pPagesStackedLayout为空", L"GMEP", L"2024-01-30");
        ProjectComponentEditingPage *projectComponentEditingPage = static_cast<ProjectComponentEditingPage*>(m_pPagesStackedLayout->widget(PROJECT_COMPONENT_EDITING_PAGE_INDEX));
        if (projectComponentEditingPage)
        {
            ShowMask();
            projectComponentEditingPage->CheckIn(projectId, enterpriseId, componentId, componentVersion, componentName);
        }
    }
}
